<template>
	<div>
		<CustomDialog
			:title="state.dialog.title"
			v-model:visible="state.dialog.isShowDialog"
			dialogWidth="900px"
			@close="closeDialog"
			:dialogFooterBtn="true"
		>
			<div class="layout-padding-auto layout-scroll-padding">
				<div class="search mb15">
					<el-row :gutter="10">
						<el-col :xs="24" :sm="6" :md="6" :lg="6" :xl="6">
							<el-form-item label="活动名称:">
								<el-input v-model="state.search.name" size="default" clearable placeholder="请输入活动名称" style="width: 100%"> </el-input>
							</el-form-item>
						</el-col>

						<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
							<el-form-item label="创建时间:">
								<el-date-picker
									type="datetimerange"
									v-model="state.search.searchDateTime"
									start-placeholder="创建开始时间"
									end-placeholder="创建时间"
									style="width: 100%"
								/>
							</el-form-item>
						</el-col>

						<el-col :xs="24" :sm="6" :md="6" :lg="6" :xl="6">
							<el-form-item class="table-form-btn" label-width="10px">
								<el-button size="default" type="primary" class="ml10" @click="getTableData">
									<el-icon>
										<ele-Search />
									</el-icon>
									查询
								</el-button>
							</el-form-item>
						</el-col>
					</el-row>
				</div>

				<el-table :data="state.tableData.data" v-loading="state.tableData.loading" @selection-change="handleSelectionChange" style="width: 100%">
					<el-table-column type="selection" width="55" />
					<el-table-column prop="couponName" label="优惠券名称" width="150"></el-table-column>
					<el-table-column prop="couponType" label="类型" width="100">
						<template #default="scope">
							<el-tag v-if="scope.row.couponType == 0">满减券</el-tag>
							<el-tag v-else>折扣券</el-tag>
						</template>
					</el-table-column>
					<el-table-column prop="rangeType" label="可用范围" width="150">
						<template #default="scope">
							<el-tag v-if="scope.row.rangeType == 0">全场通用</el-tag>
							<el-tag v-else-if="scope.row.rangeType == 1">指定商品可用</el-tag>
							<el-tag v-else-if="scope.row.rangeType == 2">指定商品不可用</el-tag>
							<el-tag v-else>指定分类</el-tag>
						</template>
					</el-table-column>
					<el-table-column prop="discountContentView" label="优惠内容" width="250"></el-table-column>
					<el-table-column prop="couponStatus" label="领券状态" width="100"> </el-table-column>
					<el-table-column prop="totalQuantity" label="发卷总数" width="100"></el-table-column>
					<el-table-column prop="totalQuantity" label="已领取" width="100"></el-table-column>
					<el-table-column prop="totalQuantity" label="已使用" width="100"></el-table-column>
					<el-table-column prop="totalQuantity" label="剩余" width="100"></el-table-column>
					<el-table-column prop="couponStatus" label="状态" width="120">
						<template #default="scope">
							<el-tag v-if="scope.row.couponStatus == 0">公开发放</el-tag>
							<el-tag v-else-if="scope.row.couponStatus == 1">后台发放</el-tag>
							<el-tag v-else-if="scope.row.couponStatus == 2">禁止使用</el-tag>
						</template>
					</el-table-column>
					<el-table-column prop="updateTime" label="修改时间" width="180"></el-table-column>
					<el-table-column prop="updateUsername" label="操作人" width="100"></el-table-column>
					<el-table-column label="操作" width="200" fixed="right">
						<template #default="scope">
							<el-button :disabled="scope.row.roleName === '超级管理员'" size="small" text type="primary" @click="onChoice(scope.row)"
								>选择</el-button
							>
						</template>
					</el-table-column>
				</el-table>
			</div>
			<el-pagination
				@size-change="onHandleSizeChange"
				@current-change="onHandleCurrentChange"
				:page-sizes="[10, 15, 20, 30, 50, 100]"
				v-model:current-page="state.tableData.paging.currentPage"
				background
				v-model:page-size="state.tableData.paging.pageSize"
				layout="total, sizes, prev, pager, next, jumper"
				:total="state.tableData.paging.totalCount"
			>
			</el-pagination>

			<template #footer>
				<span class="dialog-footer">
					<el-button @click="closeDialog" size="default">取 消</el-button>
					<el-button type="primary" @click="onBatchChoice" size="default">{{ state.dialog.submitTxt }}</el-button>
				</span>
			</template>
		</CustomDialog>
	</div>
</template>

<script setup lang="ts">
import { defineAsyncComponent, onMounted, reactive, ref } from 'vue';
import { CouponApi } from '/@/api/shop/coupon/index';
import { ElMessageBox, ElMessage } from 'element-plus';

const emits = defineEmits(['choice']);
const state = reactive({
	isToggle: false,
	search: {
		name: '',
		searchDateTime: '',
	},
	tableData: {
		data: [],
		total: 0,
		loading: false,
		paging: {
			currentPage: 1,
			pageSize: 15,
			totalCount: 0,
			totalPages: 0,
		},
	},
	dialog: {
		isShowDialog: false,
		title: '选择优惠券',
		submitTxt: '确认',
	},
});
let ccs: ChoiceCoupon[] = [];
const onRefresh = () => {
	onHandleCurrentChange(1);
};

// 分页改变
const onHandleSizeChange = (pageSize: number) => {
	state.tableData.paging.pageSize = pageSize;
	getTableData();
};
// 分页改变
const onHandleCurrentChange = (currentPage: number) => {
	state.tableData.paging.currentPage = currentPage;
	getTableData();
};

const getTableData = async () => {
	const { data } = await CouponApi().search({
		couponName: state.search.name,
		startTime: resolveStartTime(state.search.searchDateTime),
		endTime: resolveEndTime(state.search.searchDateTime),
		offset: (state.tableData.paging.currentPage - 1) * state.tableData.paging.pageSize,
		limit: state.tableData.paging.pageSize,
	});
	state.tableData.data = data.datas;
	state.tableData.paging.currentPage = data.paging.currentPage;
	state.tableData.paging.totalCount = data.paging.totalCount;
	console.log('data: ', data);
};

const resolveStartTime = (searchDateTime: any) => {
	if (state.search.searchDateTime) {
		return state.search.searchDateTime[0].getTime();
	}
};

const resolveEndTime = (searchDateTime: any) => {
	if (state.search.searchDateTime) {
		return state.search.searchDateTime[1].getTime();
	}
};

const closeDialog = () => {
	// CouponRef.value.closeDialog();
	state.dialog.isShowDialog = false;
	ccs = [];
};

const onChoice = (row: any) => {
	console.log('onChoice： ', row);
	let cc: ChoiceCoupon = {
		couponId: row.id,
		couponName: row.couponName,
		discountContentView: row.discountContentView,
		buy: row.discountContentVO.buy,
		collectStartTime: row.collectStartTime,
		collectEndTime: row.collectEndTime,
		couponValidPeriodContent: row.couponValidPeriodContent,
	};
	emits('choice', [cc]);
	closeDialog();
};

const onBatchChoice = () => {
	console.log('onBatchChoice');
	emits('choice', ccs);
	closeDialog();
};
const onOpen = () => {
	console.log('open');
	getTableData();
	state.dialog.isShowDialog = true;
};

const handleSelectionChange = (val: any[]) => {
	console.log('handleSelectionChange: ', val);
	ccs = [];
	val.forEach((e) => {
		ccs.push({
			couponId: e.id,
			couponName: e.couponName,
			discountContentView: e.discountContentView,
			buy: e.discountContentVO.buy,
			collectStartTime: e.collectStartTime,
			collectEndTime: e.collectEndTime,
			couponValidPeriodContent: e.couponValidPeriodContent,
		});
	});
};

defineExpose({ onOpen });
</script>

<style scoped lang="scss">
.table-form-btn-toggle {
	white-space: nowrap;
	user-select: none;
	display: flex;
	align-items: center;
	color: var(--el-color-primary);
}

.pandect {
	background-color: white;
	width: 99.5%;
}

.card {
	height: 80px;
	background: #fdfdfd;
	border: 1px solid #f3f3f3;
	box-shadow: 0 2px 6px #8c8c8c1f;
	border-radius: 8px;
	padding: 16px;
	margin-bottom: 16px;
}

.num {
	font-weight: 400;
	font-size: 24px;
	line-height: 26px;
	color: #434343;
}

.name {
	font-weight: 400;
	font-size: 12px;
	line-height: 14px;
	color: #595959;
}

.search {
	background-color: white;
	padding: 0 16px;
}
</style>
